﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.BackstageManagement.Views;
using MyVoteMVC.Domain;
using MyVoteMVC.Service;
using MyVoteMVC.Common;

namespace MyVoteMVC.BackstageManagement.Controllers
{
    public class QuestionController:IQuestionController
    {
        private IQuestionService questionService;
        IQuestionView view;
        public QuestionController(IQuestionView view)
        {
            this.view = view;
            questionService = Factory.GetInstance<IQuestionService>(); ;
        }

        public bool CreateQuestion(string activityID,
            string questionTitle,
            string questionContent,
            int mutilCount,
            int IfMustAnswer, 
            int IfMultiItems)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(questionTitle))
            {
                view.OnSubmitError("标题不为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(questionContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return false;
            //}

            //check length
            if (Encoding.Default.GetByteCount(questionTitle) > 100)
            {
                view.OnSubmitError("标题过长");
                return false;
            }

            if (IfMultiItems == 1 && mutilCount <= 0)
            {
                view.OnSubmitError("多选数目必须大于0");
                return false;
            }

            questionService.CreateQuestion(
                activityID,
                questionTitle,
                questionContent,
                IfMustAnswer,
                IfMultiItems,
                "admin",
                mutilCount);
            view.CreateQuestionComplete("创建成功");
            return true;
        }

        public bool EditQuestion(string activityID, 
            string questionID, 
            string questionTitle,
            string questionContent,
               int mutilCount,
            int IfMustAnswer, 
            int IfMultiItems, 
            int questionStatus)
        {
            //check is null
            if (string.IsNullOrWhiteSpace(questionTitle))
            {
                view.OnSubmitError("标题不为空");
                return false;
            }
            //if (string.IsNullOrWhiteSpace(questionContent))
            //{
            //    view.OnSubmitError("内容不为空");
            //    return false;
            //}

            //check length
            if (Encoding.Default.GetByteCount(questionTitle) > 100)
            {
                view.OnSubmitError("标题过长");
                return false;
            }

            if (IfMultiItems == 1 && mutilCount <= 0)
            {
                view.OnSubmitError("多选数目必须大于0");
                return false;
            }

            questionService.EditQuestion(
                activityID,
                questionID,
                questionTitle,
                questionContent,
                IfMustAnswer,
                IfMultiItems,
                questionStatus,//默认为1
                 "admin",
                 mutilCount);
            view.EditQuestionComplete("修改成功");
            return true;
        }

        public void GetQuestion(string questionID)
        {
            view.GetQuestionComplete(questionService.GetQuestion(questionID));
        }
    }
}
